package com.wondream.myframework.app.common.excel.function;

import lombok.extern.slf4j.Slf4j;
import org.springframework.expression.EvaluationContext;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.StandardEvaluationContext;

import javax.script.*;

@Slf4j
public class CfssExcelImportFunction {

    static CompiledScript compiledScript = null;

    static {
        String script = "function test(x,y){return parseFloat(x/y).toFixed(2);}test(x,y);";
        ScriptEngineManager scriptEngineManager = new ScriptEngineManager();
        ScriptEngine scriptEngine = scriptEngineManager.getEngineByName("nashorn");
        try {
            compiledScript = ((Compilable) scriptEngine).compile(script);
        } catch (ScriptException e) {
            e.printStackTrace();
        }
    }

    public static Object runSpring(String mathExp) {
        SpelExpressionParser spelExpressionParser = new SpelExpressionParser();
        EvaluationContext evaluationContext = new StandardEvaluationContext();
        evaluationContext.setVariable("x", "100");
        return spelExpressionParser.parseExpression(mathExp).getValue();
    }

    public static Object runSpring2(String mathExp, EvaluationContext context) {
        SpelExpressionParser spelExpressionParser = new SpelExpressionParser();
        return spelExpressionParser.parseExpression(mathExp).getValue(context);
    }

    public static Object runJs(String mathExp) {
        String script = "var obj={run:function(){return parseFloat(" + mathExp + ").toFixed(2)}}";
        ScriptEngineManager scriptEngineManager = new ScriptEngineManager();
        ScriptEngine scriptEngine = scriptEngineManager.getEngineByName("nashorn");
        try {
            scriptEngine.eval(script);
            Object object = scriptEngine.get("obj");
            return ((Invocable) scriptEngine).invokeMethod(object, "run");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static Object runJs2(String mathExp) {
        String script = "var obj={run:function(){return parseFloat(" + mathExp + ").toFixed(2)}}";
        ScriptEngineManager sem = new ScriptEngineManager();
        ScriptEngine scriptEngine = sem.getEngineByName("nashorn");
        try {
            scriptEngine.eval(script);
            Object obj = scriptEngine.get("obj");
            return ((Invocable) scriptEngine).invokeMethod(obj, "run");
        } catch (Exception e) {
            log.error("runJs2 error:" + e.getMessage());
        }
        return null;
    }

    public static void main(String[] args) {
        String mathExp = "(20.0/3.0)";
        String mathExp2 = "(20/3)";
        EvaluationContext context = new StandardEvaluationContext();
        context.setVariable("x", 20.0);
        context.setVariable("y", 3.0);
        String.valueOf(20.0/3.0);
        System.out.println(runSpring2("new java.math.BigDecimal(T(java.lang.String).valueOf(#x/#y))", context));
        for (int i = 0; i < 1000; i++) {
            Bindings bindings = new SimpleBindings();
            bindings.put("x", "20.0");
            bindings.put("y", "3.0");
            try {
                System.out.println(compiledScript.eval(bindings));
            } catch (ScriptException e) {
                e.printStackTrace();
            }
        }
    }
}
